﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.ComponentModel;

namespace HomeMediaBrowser
{
    /// <summary>
    /// Interaction logic for MediaPlayerPage.xaml
    /// </summary>
    public partial class MediaPlayerPage : PageFunction<String>,INotifyPropertyChanged
    {
        public MediaPlayerPage()
        {
            InitializeComponent();
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public double MediaTime
        {
            get 
            { 
                if (mediaPlayerMain == null)
                    return 0;
                return mediaPlayerMain.Position.TotalMilliseconds;
            }
            set
            {
                TimeSpan ts = new TimeSpan(0, 0, 0, 0, (int)value);
                mediaPlayerMain.Position = ts;
                NotifyPropertyChanged("MediaTime");
            }
        }

        public int MediaVolume
        {
            get
            {
                return (int)(sliderVolume.Value * 100);
            }
            set
            {
                sliderVolume.Value = (int)(value / 100.0);
            }
        }

        enum ePlayState { stop, play, pause }

        public string m_strFilePath;
        private ePlayState m_ePlayState;

        public virtual void OnLoaded(object sender, RoutedEventArgs e)
        {
            OnInitDialog();
        }
        DispatcherTimer m_proTimer = new DispatcherTimer();

        void OnInitDialog()
        {
            Button_Pause = ControlPanel.Template.FindName("Button_Pause", ControlPanel) as Button;
            Button_Play = ControlPanel.Template.FindName("Button_Play", ControlPanel) as Button;
            MediaControlPanel = ControlPanel.Template.FindName("MediaControlPanel", ControlPanel) as Canvas;
            sliderTime = ControlPanel.Template.FindName("sliderTime", ControlPanel) as Slider;
            sliderVolume = ControlPanel.Template.FindName("sliderVolume", ControlPanel) as Slider;
            sliderTime.DataContext = this;
            PlayerVolumeBar = ControlPanel.Template.FindName("PlayerVolumeBar", ControlPanel) as Grid;
            
            PlayerVolumeBar.Visibility = Visibility.Hidden;

            double defVolume = 80;
            mediaPlayerMain.Volume = defVolume;
            sliderVolume.Value = defVolume;
        
            UpdatePlayState(ePlayState.stop);
        }

        public Button Button_Pause;
        public Button Button_Play;
        public Canvas MediaControlPanel;
        public Slider sliderTime;
        public Slider sliderVolume;
        public Grid PlayerVolumeBar;

        void SyncProgressTimer(object sender, EventArgs e)
        {
            //sliderTime.Value = (double)mediaPlayerMain.Position.TotalMilliseconds;
            NotifyPropertyChanged("MediaTime");
        }

        // current state
        void UpdatePlayState(ePlayState state)
        {
            m_ePlayState = state;

            // show play
            bool bShowPlay = m_ePlayState == ePlayState.pause || m_ePlayState == ePlayState.stop;

            Button_Pause.Visibility = bShowPlay ? Visibility.Hidden : Visibility.Visible;
            Button_Play.Visibility = bShowPlay ? Visibility.Visible : Visibility.Hidden;

            if (!bShowPlay) m_proTimer.Start();
            else m_proTimer.Stop();
        }

        private void mediaPlayerMain_MediaOpened(object sender, RoutedEventArgs e)
        {
            sliderTime.Value = 0;
            sliderTime.Minimum = 0;

            if (mediaPlayerMain.NaturalDuration.HasTimeSpan)
            {
                sliderTime.Maximum = mediaPlayerMain.NaturalDuration.TimeSpan.TotalMilliseconds;
            }

            sliderTime.IsEnabled = mediaPlayerMain.IsLoaded;
            sliderVolume.IsEnabled = mediaPlayerMain.IsLoaded;

            UpdatePlayState(ePlayState.play);

            m_proTimer.Tick += new EventHandler(SyncProgressTimer);
            m_proTimer.Interval = TimeSpan.FromSeconds(2);
            m_proTimer.Start();
        }

        private void mediaPlayerMain_MediaEnded(object sender, RoutedEventArgs e)
        {
        }

        private void Button_Pause_Click(object sender, RoutedEventArgs e)
        {
            mediaPlayerMain.Pause();
            UpdatePlayState(ePlayState.pause);
        }

        public void play(string strfile)
        {
            m_strFilePath = strfile;
            m_ePlayState = ePlayState.stop;

            RoutedEventArgs argc = new RoutedEventArgs();
            Button_Play_Click(this, argc);
        }

        private void Button_Play_Click(object sender, RoutedEventArgs e)
        {
            if (m_strFilePath.Length == 0)
                return;

            if (m_ePlayState == ePlayState.play)
            {
                UpdatePlayState(ePlayState.pause);
                mediaPlayerMain.Pause();
            }
            else if (m_ePlayState == ePlayState.stop)
            {
                if (m_strFilePath.PadLeft(7).CompareTo("http://") == 0)
                {
                    // url;
                }

                mediaPlayerMain.Source = new Uri(m_strFilePath);
                mediaPlayerMain.Play();

                Binding b = new Binding();
                b.Mode = BindingMode.OneWay;
                b.Source = mediaPlayerMain.Volume;
                sliderVolume.SetBinding(Slider.ValueProperty, b);

                UpdatePlayState(ePlayState.play);
            }
            else if (m_ePlayState == ePlayState.pause)
            {
                mediaPlayerMain.Play();
                UpdatePlayState(ePlayState.play);
            }
        }

        private void sliderTime_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            int SliderValue = (int)sliderTime.Value;
            // Overloaded constructor takes the arguments days, hours, minutes, seconds, miniseconds.
            // Create a TimeSpan with miliseconds equal to the slider value.
            TimeSpan ts = new TimeSpan(0, 0, 0, 0, SliderValue);
            mediaPlayerMain.Position = ts;
        }

        public void Stop()
        {
            mediaPlayerMain.Close();
            sliderTime.Value = 0;
            UpdatePlayState(ePlayState.stop);
        }
        private void Button_Stop_Click(object sender, RoutedEventArgs e)
        {
            Stop();
        }

        private void ShowVolumePanel(bool bShow)
        {
             PlayerVolumeBar.Visibility = bShow ? Visibility.Visible : Visibility.Hidden;
        }

        private void Button_VolumeMore_Click(object sender, RoutedEventArgs e)
        {
            ShowVolumePanel(true);
        }
        private void OnSilderVolumeMouseLeave(object sender, RoutedEventArgs e)
        {
            ShowVolumePanel(false);
        }

        private void Button_Volume_Click(object sender, RoutedEventArgs e)
        {
        }
    }
}
